package cn.marinda.listener;

import cn.marinda.MinecraftServerHelper;
import cn.marinda.entity.ComponentSource;
import cn.marinda.gui.impl.CreateServerHelperImpl;
import cn.marinda.gui.impl.DownloadServerDiaLogImpl;
import cn.marinda.util.FileUtils;
import cn.marinda.util.ServerUtils;
import cn.marinda.util.SwingUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建服务器事件
 *
 * @author admin
 * @date 2022/07/16
 */
public class CreateServerEvent implements ActionListener {

    private JButton button;
    private JComboBox serverCore;
    private Map<String,List<JRadioButton>> radioMap = new HashMap<>();
    private Map<String,Boolean> radioDataMap = new HashMap<>();
    private Map<String,List<ComponentSource>> textFieldMap = new HashMap<>();
    private Map<String,String> textDataMap = new HashMap<>();
    /**
     * 创建服务器事件
     *
     * @param button 按钮
     */
    public CreateServerEvent(JButton button, JComboBox serverCore,Map<String,List<JRadioButton>> radioMap,Map<String,List<ComponentSource>> componentMap){
        this.button = button;
        this.serverCore = serverCore;
        this.radioMap = radioMap;
        this.textFieldMap = componentMap;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        CreateServerHelperImpl.instance.close();
        FileUtils.createServerDir();
//        赋值！
        ServerUtils.serverCore = serverCore.getSelectedItem().toString();
        setJRadioSelectData();
        setTextDataMap();
//        声明是创建服务端核心操作
        ServerUtils.downloadCoreFlag = false;
//        获取服务器配置的相关值
        SwingUtils.downloadDialog  = new DownloadServerDiaLogImpl(ServerUtils.serverCore,ServerUtils.serverPluginSet,radioDataMap,textDataMap);
        SwingUtils.downloadDialog.open();
    }

    /**
     * 设置当前单选按钮选择数据
     */
    private void setJRadioSelectData(){
        radioMap.keySet().stream().forEach(element->{
            List<JRadioButton> radioButtons = radioMap.get(element);
            boolean value = parseRadioSelect(radioButtons);
            radioDataMap.put(element,value);
        });
    }

    /**
     * 转换radio文本
     *
     * @param radioButtons 单选按钮
     * @return boolean
     */
    private boolean parseRadioSelect(List<JRadioButton> radioButtons){
        boolean flag = false;
        for(JRadioButton radioButton : radioButtons){
            if(radioButton.isSelected()){
                String text = radioButton.getText();
                if(text.equalsIgnoreCase("开启")){
                    flag = true;
                }else{
                    flag = false;
                }
            }
        }
        return flag;
    }


    /**
     * 设置输入框数据集合
     */
    private void setTextDataMap(){
        this.textFieldMap.keySet().stream().forEach(ele->{
            List<ComponentSource> componentSources = textFieldMap.get(ele);
            for(ComponentSource componentSource : componentSources){
                if(componentSource.getComponent() instanceof JTextField){
                    JTextField textField = (JTextField) componentSource.getComponent();
                    String key = componentSource.getLabel().getText();
                    String value = textField.getText();
                    textDataMap.put(key,value);
                }
            }
        });
    }


}
